//
//  DiscussViewController.m
//  OperationPro
//
//  Created by yaxue wang on 16/9/27.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DiscussViewController.h"
#import "DiscussView.h"
#import "DiscussModel.h"
#import "CreateDiscussViewController.h"
#import "DiscussDetailViewController.h"
#import "DiscussModel.h"
#import "InvitationModel.h"
#import "SVPullToRefresh.h"
#import "DiscussSearchViewController.h"
@interface DiscussViewController ()<UISearchBarDelegate>
@property (nonatomic, strong) DiscussView *mainView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong)UIScrollView *passScrollView;
@property (nonatomic, strong)UIScrollView *bigScrollView;
@end

@implementation DiscussViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self layoutContext];
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //刷新
    [self.mainView refresh];
    
}
- (void)initView
{
    [self getPass];
    [self noPass];
    self.navBar.hidden=YES;


}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return  UIStatusBarStyleLightContent;
}
- (void)layoutContext
{
    //审核通过
    if ([[CClass getCacheData:NSUD_state]isEqualToString:@"0"]) {
       
        _passScrollView.hidden=NO;
        _bigScrollView.hidden=YES;
        
    }else
    {
        _passScrollView.hidden=YES;
        _bigScrollView.hidden=NO;
        
    }
    [self.view bringSubviewToFront: self.mainTabBar ];

}
- (void)getPass
{
    self.navBar.hidden=YES;
    _passScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight-49)];
    _passScrollView.backgroundColor =BackColor;
    [self.view addSubview:_passScrollView];
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor=MainColor;
    [_passScrollView addSubview:narView];
    UILabel *titleLab =[[UILabel alloc]initWithFrame:CGRectMake((WindowWidth-70)/2, 30, 70, 26)];
    titleLab.text=@"讨论区";
    titleLab.font=F(16);
    titleLab.textColor=[UIColor whiteColor];
    titleLab.textAlignment=NSTextAlignmentCenter;
    [narView addSubview:titleLab];
    UIView *ringView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-80, 0, 80, 64)];
    [narView addSubview:ringView];
    [ringView bk_whenTapped:^{
        
        CreateDiscussViewController *createVC=[[CreateDiscussViewController alloc]init];
        [self pushController:createVC];
    }];
    UILabel *rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 33, 65, 20)];
    rightLab.text =@"发贴";
    rightLab.textAlignment=NSTextAlignmentRight;
    rightLab.font=F(14);
    rightLab.textColor=[UIColor whiteColor];
    rightLab.userInteractionEnabled=YES;
    [ringView addSubview:rightLab];
    UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WindowWidth, 0.5)];
    line.backgroundColor=LineColor;
    [narView addSubview:line];
   
    UIView *searchView=[[UIView alloc]initWithFrame:CGRectMake(15, 6+64, WindowWidth-30, 30)];
    searchView.backgroundColor=[UIColor whiteColor];
    searchView.layer.masksToBounds=YES;
    searchView.layer.cornerRadius=4;
    searchView.layer.borderColor=LineColor.CGColor;
    searchView.layer.borderWidth=0.5;
    [_passScrollView addSubview:searchView];

    UILabel*searchField = [[UILabel alloc]initWithFrame:CGRectMake(10, 2, searchView.bounds.size.width-20, 26)];
    searchField.text = @"搜索";
    searchField.textAlignment=NSTextAlignmentCenter;
    searchField.textColor=LightFontColor;
    searchField.userInteractionEnabled=YES;
    searchField.font=FF(15);
    [searchView addSubview:searchField];
    
    [searchView bk_whenTapped:^{
        DiscussSearchViewController *dicVC=[[DiscussSearchViewController alloc]init];
        [self pushController:dicVC];
    }];

    __block DiscussViewController *weakview = self;
    _mainView = [[DiscussView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(searchView.frame)+6, WindowWidth, _passScrollView.bounds.size.height-CGRectGetMaxY(searchView.frame)-6)];
    [_passScrollView addSubview:_mainView];
    [_mainView setGetData:^(NSInteger num) {
    if (num==0) {
            searchView.hidden=YES;
        }else
        {
            searchView.hidden=NO;
        }

    }];
       __block typeof(self) blockself = self;
    
   
    [_mainView setCCellSelect:^(DiscussModel *model) {
       weakview.searchBar.text=@"";
        DiscussDetailViewController *disscussVC=[[DiscussDetailViewController alloc]init];
        NSString *warn =model.D_warning_uuid;
        if (warn==nil) {
#pragma mark  贴子的详情
            [blockself setIsLoading:YES];
            [CClass HttpRequestTask:YES withUrl:@"discuss_detail" withData:@{@"uuid":model.D_uuid} notifyError:YES onSuccess:^(BaseModel *model) {
                [blockself setIsLoading:NO];
                disscussVC.invitationModel=[[InvitationModel alloc] initWithDictionary:model.B_data error:nil];

                disscussVC.isWarnuiud=NO;

                [weakview pushController:disscussVC];
                
            } onError:^(BaseModel *model) {
                [blockself setIsLoading:NO];

            }];
            
        }else
        {
#pragma mark  贴子的详情
            [blockself setIsLoading:YES];
            [CClass HttpRequestTask:YES withUrl:@"discuss_detail" withData:@{@"uuid":model.D_uuid} notifyError:YES onSuccess:^(BaseModel *model) {
#pragma mark  保修单的详情
                disscussVC.invitationModel=[[InvitationModel alloc] initWithDictionary:model.B_data error:nil];
                [CClass HttpRequestTask:YES withUrl:@"warninglist_orderContent" withData:@{@"warningUuid":warn} notifyError:YES onSuccess:^(BaseModel *model) {
                    [blockself setIsLoading:NO];
                    
                    disscussVC.imageString =[model.B_data objectForKey:@"image"];
                    disscussVC.describe =[model.B_data objectForKey:@"describe"];
                    disscussVC.warnuiud =warn;
                    disscussVC.isWarnuiud=YES;
                    [weakview pushController:disscussVC];
                    
                } onError:^(BaseModel *model) {
                    [blockself setIsLoading:NO];

                }];
                
                
            } onError:^(BaseModel *model) {
                [blockself setIsLoading:NO];

            }];
            
        }
        
       [blockself.searchBar resignFirstResponder];
    }];
    [_mainView setDiscussBecomeFirstResponse:^{
        [blockself.searchBar resignFirstResponder];
    }];
  
}
- (void)noPass
{
   
   _bigScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight-48)];
    _bigScrollView.backgroundColor =[UIColor clearColor];
    [self.view addSubview:_bigScrollView];
    _bigScrollView.tag=200;
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor=MainColor;
    [_bigScrollView addSubview:narView];
    
 
    UILabel *titleLab =[[UILabel alloc]initWithFrame:CGRectMake((WindowWidth-70)/2, 30, 70, 26)];
    titleLab.text=@"讨论区";
    titleLab.font=F(16);
    titleLab.textColor=[UIColor whiteColor];
    titleLab.textAlignment=NSTextAlignmentCenter;
    [narView addSubview:titleLab];
    UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WindowWidth, 0.5)];
    line.backgroundColor=LineColor;
    [narView addSubview:line];
    UITableView *tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64)];
    tableView.backgroundColor =[UIColor clearColor];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    tableView.tableHeaderView=[self HeaderView];
    tableView.showsPullToRefresh=YES;
    tableView.showsVerticalScrollIndicator=NO;
    self.navigationController.navigationBar.translucent = NO;
    
    
    __block typeof(tableView) blockself = tableView;
    [tableView addPullToRefreshWithActionHandler:^{
        
        
        NSString *uuid =[CClass getCacheData:NSUD_UUID];
        if ([uuid isEqualToString:@""]) {
            
        }else
        {
            [CClass HttpRequestTask:YES withUrl:@"reviseaccount_selectCustomer" withData:@{@"uuid":[CClass getCacheData:NSUD_UUID]} notifyError:YES onSuccess:^(BaseModel *model) {
                
                [CClass saveCacheData:[model.B_data objectForKey:@"state"] key:NSUD_state];
                //0已审核; 1待审核
                [self layoutContext];
                // 完成时调用
                [blockself.pullToRefreshView stopAnimating];
                blockself.pullToRefreshView.textColor =[UIColor grayColor];
                blockself.pullToRefreshView.arrowColor =[UIColor grayColor];
            } onError:^(BaseModel *model) {
                [blockself.pullToRefreshView stopAnimating];
                
            }];
        }
        
    }];
    [_bigScrollView addSubview:tableView];

   
}
- (UIView*)HeaderView{
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 400)];
    headView.backgroundColor=BackColor;
    UIImage *img =[UIImage imageNamed:@"Ico_register_noThrough"];
    UIImageView *throughImg =[[UIImageView alloc]initWithFrame:CGRectMake((WindowWidth-img.size.width)/2,98, img.size.width, img.size.height)];
    [headView addSubview:throughImg];
    
    
    throughImg.image =img;
    UILabel *nameLab =[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(throughImg.frame)+10, WindowWidth, 20)];
    [headView addSubview:nameLab];
    
    nameLab.font=F(15);
    nameLab.textColor =MainColor;
    nameLab.textAlignment =NSTextAlignmentCenter;
    nameLab.text =@"尚未通过单位审核，请联系主管处理";
    return headView;
    
}




@end
